// ---------------------------------------------------------------------
//
// -- 声音管理
// -- author £
//
// ---------------------------------------------------------------------

const SoundInfo = require('SoundInfo');

let _soundList = null,
    _seList = null,
    _soundLib = null,
    _seLib = null,
    _sounds = [],
    _ses = [],
    _yys = [],
    _bgList = null,
    _iBg = -1;

let SoundManager = {

    init: function (sounds, ses) {
        // SoundInfo.startup();
        if (SoundInfo.invalid()) return;

        let len, i, cur;

        _soundList = sounds;
        _seList = ses;
        len = _soundList.length;
        _soundLib = {};

        for (i = 0; i < len; ++i) {
            cur = _soundList[i];
            _soundLib[this.getClipName(cur)] = cur;
        }

        len = _seList.length;
        _seLib = {};

        for (i = 0; i < len; ++i) {
            cur = _seList[i];
            _seLib[this.getClipName(cur)] = cur;
        }
    },

    clearData: function () {
        cc.audioEngine.stopAll();
        cc.audioEngine.uncacheAll();
        _soundList = null;
        _seList = null;
        _soundLib = null;
        _seLib = null;
        _sounds.length = 0;
        _ses.length = 0;
        _yys.length = 0;
        _bgList = null;
        _iBg = -1;
    },

    getClipName: function (str) {
        if (SoundInfo.invalid()) return;

        return str.substring(str.lastIndexOf('/') + 1, str.lastIndexOf('.'));
    },

    setSoundVol: function (value) {
        if (SoundInfo.invalid()) return;

        SoundInfo.setSoundVol(value);

        if (SoundInfo.getSoundMute()) return;

        if (_yys && _yys.length) return;

        let len = _sounds.length;
        
        for (let i = 0; i < len; ++i) {
            cc.audioEngine.setVolume(_sounds[i], value);
        }
    },

    setSEVol: function (value) {
        if (SoundInfo.invalid()) return;

        SoundInfo.setSEVol(value);

        if (SoundInfo.getSEMute()) return;

        if (_yys && _yys.length) return;

        let len = _ses.length;
        
        for (let i = 0; i < len; ++i) {
            cc.audioEngine.setVolume(_ses[i], value);
        }
    },

    setSoundMute: function (value) {
        if (SoundInfo.invalid()) return;

        SoundInfo.setSoundMute(value);

        let len = _sounds.length;
        
        for (let i = 0; i < len; ++i) {
            cc.audioEngine.setVolume(_sounds[i], SoundInfo.getSoundVol());
        }
    },

    setSEMute: function (value) {
        if (SoundInfo.invalid()) return;

        SoundInfo.setSEMute(value);

        let len = _ses.length;
        
        for (let i = 0; i < len; ++i) {
            cc.audioEngine.setVolume(_ses[i], SoundInfo.getSEVol());
        }

    },

    playSound: function (key) {
        if (SoundInfo.invalid()) return;

        if (SoundInfo.getSoundMute()) return;

        let id = cc.audioEngine.play(_soundLib[key], false, 
            SoundInfo.getSoundVol());
        
        _sounds[_sounds.length] = id;
        cc.audioEngine.setFinishCallback(id, function () {
            if (!_sounds || !_sounds.length) return;

            let index = _sounds.indexOf(id);

            _sounds.splice(index, 1);
        });
    },

    playSE: function (key, fullPath) {
        if (SoundInfo.invalid()) return;

        if (SoundInfo.getSEMute()) return;
        
        // console.log('se path:', fullPath ? key : _seLib[key]);
        // console.log('se vol:', SoundInfo.getSEVol());
        let id = cc.audioEngine.play(fullPath ? key : _seLib[key], false, 
            SoundInfo.getSEVol());
            // console.log('se id:', id);
        _ses[_ses.length] = id;
        // console.log('ses:', _ses);
        cc.audioEngine.setFinishCallback(id, function () {
            // console.log('se finish0:', id);
            if (!_ses || !_ses.length) return;

            let index = _ses.indexOf(id);

            _ses.splice(index, 1);
            // console.log('se finish1:', _ses);
        });
    },

    playYY: function (key) {
        let len, i;

        if (SoundInfo.invalid()) return;
        
        if (SoundInfo.invalid()) return;
        
        if (!SoundInfo.getSoundMute()) {
            len = _sounds.length;
            
            for (i = 0; i < len; ++i) {
                cc.audioEngine.setVolume(_sounds[i], 0);
            }
        }

        if (!SoundInfo.getSEMute()) {
            len = _ses.length;
            
            for (i = 0; i < len; ++i) {
                cc.audioEngine.setVolume(_ses[i], 0);
            }
        }

        let id = cc.audioEngine.play(key, false, 
            SoundInfo.getSEVol());

        _yys[_yys.length] = id;
        cc.audioEngine.setFinishCallback(id, function () {
            if (!_yys || !_yys.length) return;

            let index = _yys.indexOf(id);

            _yys.splice(index, 1);

            if (_yys.length === 0) {
                if (!SoundInfo.getSoundMute()) {
                    len = _sounds.length;
                    
                    for (let j = 0; j < len; ++j) {
                        cc.audioEngine.setVolume(_sounds[j], SoundInfo.getSoundVol());
                    }
                }
        
                if (!SoundInfo.getSEMute()) {
                    len = _ses.length;
                    
                    for (let k = 0; k < len; ++k) {
                        cc.audioEngine.setVolume(_ses[k], SoundInfo.getSEVol());
                    }
                }
            }
        });
    },

    /** 背景音乐循环播放 */
    playBgList: function (list) {
        if (SoundInfo.invalid()) return;

        _bgList = list;
        _iBg = -1;

        if (!_bgList || !_bgList.length) return;

        this.playNextBg(-1);
    },

    /** 播放下一首 */
    playNextBg: function (lastID) {
        if (SoundInfo.invalid()) return;
        // if (!_sounds || !_sounds.length) return;
        if (lastID >= 0) {
            if (!_sounds || !_sounds.length) return;

            let index = _sounds.indexOf(id);
            _sounds.splice(index, 1);
        }
        
        ++_iBg;

        if (_iBg === _bgList.length)
            _iBg = 0;

        let id = cc.audioEngine.play(_soundLib[_bgList[_iBg]], false, 
            SoundInfo.getSoundVol());

        _sounds[_sounds.length] = id;
        cc.audioEngine.setFinishCallback(id, (function () {
            this.playNextBg(id);
        }).bind(this));
    },

};

module.exports = SoundManager;